﻿Imports System.ComponentModel
Imports System.Xml

Module modStart

    Private m_StartTime As String = "" 'yyyyMMddHHmmss
    Private m_StopTime As String = "" 'yyyyMMddHHmmss

    <Description("執行程式")> _
    Sub Main()
        Dim sCMD As String = ""
        Dim oLog As New LogHandler.clsLogger
        Dim oTrans As New clsTransferUtility

        '開始轉檔前,取得起迄的時間
        GetTransferInterval()
        sCMD = Command()

        Select Case sCMD.ToLower
            Case "form"
                Dim o As New frmMain
                o.ShowDialog()

            Case Else

                If sCMD.IndexOf("txt") >= 0 Then
                    Dim sFile As String = ""
                    If sCMD.IndexOf(";") >= 0 Then
                        oTrans.init(My.Settings.LogFolder)
                        System.Console.WriteLine("start to transfer")
                        sFile = sCMD.Split(";")(1)
                        oTrans.TransFromTxtFile(sFile)
                    Else
                        System.Console.WriteLine("Transfer from txt file using Ex: txt;c:\test\docid.txt")
                        System.Console.WriteLine("press any key to exit ")
                        System.Console.ReadKey()
                    End If

                    Exit Sub
                End If

                If sCMD.ToLower = "s" OrElse My.Settings.Item("CurrDoc") = "" Then
                    Dim o As New frmSET
                    o.ShowDialog()
                    If o.isSave = False Then
                        System.Console.WriteLine(" cancel transfer ")
                        System.Console.WriteLine(" press any key to exit ")
                        System.Console.ReadKey()
                        Exit Sub
                    End If
                    GetTransferInterval()
                End If

StartTrans:
                Dim totalDoc As Integer = 0
                oTrans.init(My.Settings.LogFolder)
                System.Console.WriteLine("start to transfer")
                oLog.writeLine(My.Settings.LogFolder, "Static", "Start")
                Console.WriteLine(m_StartTime)
                Console.WriteLine(m_StopTime)
                oLog.writeLine(My.Settings.LogFolder, "Static", "Transfer Time " & m_StartTime & " ~ " & m_StopTime)
                
                While Double.Parse(My.Settings.CurrDoc) < Double.Parse(My.Settings.DocEnd)
                    'Check FileNet Backup
                    Console.WriteLine("Check FileNet Backup")
                    CheckFileNetBackup()
                    'Check Stop Time
                    Console.WriteLine(" Check Transfer STOP")
                    If Now.ToString("yyyyMMddHHmmss") < m_StartTime Then
                        Console.WriteLine("Wait Start")
                        waitStart()
                    ElseIf Now.ToString("yyyyMMddHHmmss") > m_StopTime Then
                        Console.WriteLine("Wait Start")
                        waitStart()
                        oLog.writeLine(My.Settings.LogFolder, "Static", "Total Doc Ct " & totalDoc)
                        oLog.writeLine(My.Settings.LogFolder, "Static", "End")
                        GoTo StartTrans
                    End If

                    oTrans.doTransferByDocNo(Double.Parse(My.Settings.DocStart), _
                                             Double.Parse(My.Settings.DocEnd), _
                                             Double.Parse(My.Settings.CurrDoc))
                    totalDoc = totalDoc + oTrans.docCt
                End While
                oLog.writeLine(My.Settings.LogFolder, "Static", "Total Doc Ct " & totalDoc)
                oLog.writeLine(My.Settings.LogFolder, "Static", "End")
        End Select
        System.Console.WriteLine("Transfer End")
    End Sub

    Private Function CheckExistDocClass(ByVal sClass As String) As Boolean
        Dim xmlDoc As New Xml.XmlDocument
        Dim xmlNodeList As XmlNodeList
        xmlDoc.Load("IStoCE.xml")
        xmlNodeList = xmlDoc.GetElementsByTagName("DocClass")
        For i As Integer = 0 To xmlNodeList.Count - 1
            If xmlNodeList.Item(i).Attributes("IS").Value = sClass Then
                Return True
            End If
        Next
        Return False
    End Function

    Private Sub GetTransferInterval()
        m_StartTime = Date.Parse(Now.ToString("yyyy/MM/dd") & " " & My.Settings.StartTime).ToString("yyyyMMddHHmmss")
        If My.Settings.StopTime < My.Settings.StartTime Then
            Select Case Weekday(Now, FirstDayOfWeek.Sunday)

                Case 6
                    m_StopTime = DateAdd(DateInterval.Day, 2, Now).ToString("yyyyMMdd") & Replace(My.Settings.StopTime, ":", "")
                Case Else
                    m_StopTime = Date.Parse(DateAdd(DateInterval.Day, 1, Now).ToString("yyyy/MM/dd") & " " & My.Settings.StopTime).ToString("yyyyMMddHHmmss")
            End Select

        Else
            m_StopTime = Date.Parse(Now.ToString("yyyy/MM/dd") & " " & My.Settings.StopTime).ToString("yyyyMMddHHmmss")
        End If
        
    End Sub


    Private Sub waitStart()
        GetTransferInterval
        While True
            System.Threading.Thread.Sleep(5000)
            Console.Write(".")
            If Now.ToString("yyyyMMddHHmmss") > m_StartTime Then
                Console.Clear()
                Exit While
            End If
        End While
    End Sub

    Private Sub CheckFileNetBackup()
        Dim iDuration As Integer
        If Integer.TryParse(My.Settings.BackupDuration, iDuration) = False Then
            iDuration = 60
        End If
        Dim FileNetBackupStartTime As String = ""
        Dim FileNetBackupEndTime As String = ""
        FileNetBackupStartTime = Now.ToString("yyyyMMdd") & My.Settings.FileNetBackupTime.ToString.Replace(":", "")
        FileNetBackupEndTime = DateAdd(DateInterval.Minute, iDuration, CDate(Now.ToString("yyyy/MM/dd") & " " & My.Settings.FileNetBackupTime)).ToString("yyyyMMddHHmmss")

        Console.WriteLine(FileNetBackupStartTime)
        Console.WriteLine(FileNetBackupEndTime)

        If Now.ToString("yyyyMMddHHmmss") >= FileNetBackupEndTime Then
            'DO Nothing
        Else
            If Now.ToString("yyyyMMddHHmmss") >= FileNetBackupStartTime Then
                For iMinute As Integer = 1 To iDuration
                    For iSec As Integer = 1 To 60
                        System.Threading.Thread.Sleep(1000)
                    Next
                Next
            End If
        End If

    End Sub
End Module
